package persistencia;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import contas.ContaEspecial;
import contas.ContaPoupanca;
import contas.ContaSimples;

public class DAOConta implements DAOContaInterface {
	private Connection con = null;
	private Statement st = null;
	private ResultSet rs = null;
	
	public DAOConta() throws Exception{
		con= Conexao.getConexao();
		st=con.createStatement();
	}
	
	public void incluirContaEspecial(ContaEspecial conta) throws Exception {
	st.executeUpdate("insert into ContaEspecial values("+conta.getNumConta()+",'"+conta.getNome()+"',"+conta.getSaldo()+","+conta.getLimite()+","+conta.getTempoDeConta()+")");
	}

	public void excluirContaEspecial(int numConta) throws Exception {
	st.executeUpdate("delete from ContaEspecial where numConta="+numConta+"");
	}

	public ContaEspecial consultarContaEspecial(int numConta) throws Exception {
		ContaEspecial conta =null;
		rs=st.executeQuery("select * from ContaEspecial where numConta="+numConta+"");
		if(rs.next()){
			conta= new ContaEspecial();
			conta.setNome(rs.getString(1));
			conta.setNumConta(rs.getInt(2));
			conta.setSaldo(rs.getDouble(3));
			conta.setLimite(rs.getDouble(4));
			conta.setTempoDeConta(rs.getInt(5));
			
		}
		return conta;
	}
	public void alterarContaEspecial(ContaEspecial conta) throws Exception {
		st.executeUpdate("update ContaEspecial set nome='"+conta.getNome()+"',saldo="+conta.getSaldo()+",limite="+conta.getLimite()+",tempodeconta="+conta.getTempoDeConta()+" where numConta="+conta.getNumConta()+" ");
	}

	
	public void incluirContaPoupanca(ContaPoupanca conta) throws Exception {
		st.executeUpdate("insert into ContaPoupanca values("+conta.getNumConta()+",'"+conta.getNome()+"',"+conta.getSaldo()+","+conta.getTaxa()+","+conta.getTempoDeConta()+")");
	}
	public void excluirContaPoupanca(int numConta) throws Exception {
		st.executeUpdate("delete from ContaPoupanca where numConta="+numConta+"");
	}
	public ContaPoupanca consultarContaPoupanca(int numConta) throws Exception {
		ContaPoupanca conta =null;
		rs=st.executeQuery("select * from contaPoupanca where numConta="+numConta+"");
		if(rs.next()){
			conta= new ContaPoupanca();
			conta.setNumConta(rs.getInt(1));
			conta.setNome(rs.getString(2));
			conta.setSaldo(rs.getDouble(3));
			conta.setTaxa(rs.getDouble(4));
			conta.setTempoDeConta(rs.getInt(5));
		}
			return conta;
	}
	public void alterarContaPoupanca(ContaPoupanca conta) throws Exception {
		st.executeUpdate("update ContaPoupanca set nome='"+conta.getNome()+"',saldo="+conta.getSaldo()+",taxa="+conta.getTaxa()+" ,tempodeconta="+conta.getTempoDeConta()+"where numConta="+conta.getNumConta()+"");
	}

	
	public void incluirContaSimples(ContaSimples conta) throws Exception {
		st.executeUpdate("insert into ContaSimples values("+conta.getNumConta()+",'"+conta.getNome()+"',"+conta.getSaldo()+","+conta.getTempoDeConta()+")");
	}
	public void excluirContaSimples(int numConta) throws Exception {
		st.executeUpdate("delete from ContaSimples where numConta="+numConta+"");
	}

	public ContaSimples consultarContaSimples(int numConta) throws Exception {
		ContaSimples conta =null;
		rs=st.executeQuery("select * from ContaSimples where numConta="+numConta+"");
		if(rs.next()){
			conta= new ContaSimples();
			conta.setNumConta(rs.getInt(1));
			conta.setNome(rs.getString(2));
			conta.setSaldo(rs.getDouble(3));
			conta.setTempoDeConta(rs.getInt(4));
		}
			return conta;
	}
	public void alterarContaSimples(ContaSimples conta) throws Exception {
		st.executeUpdate("update ContaSimples set nome='"+conta.getNome()+"',saldo="+conta.getSaldo()+",tempodeconta="+conta.getTempoDeConta()+" where numConta="+conta.getNumConta()+" ");	
	}
}
